package leecode

import (
	"strconv"
	"strings"
)

//a 97

func WordPattern(pattern string, s string) bool {

	strS := strings.Split(s, " ")
	strPattern := strings.Split(pattern, "")
	newS := newString(strS)
	newPattern := newString(strPattern)
	return newS == newPattern
	//return myPattern == pattern
}

// 解析字符串
func newString(str []string) string {
	index := 1
	wordMap := make(map[string]int, len(str)) //key为单词 , value为有a , b , c等表示的单词出现次数
	for _, key := range str {
		_, ok := wordMap[key] //判断key是否存在
		if !ok {
			wordMap[key] = index
			index++
		}
	}
	var strPattern []string
	strPattern = make([]string, 0)
	for _, value := range str {
		strPattern = append(strPattern, strconv.Itoa(wordMap[value]))
	}
	myPattern := strings.Join(strPattern, "")
	return myPattern
}
